//
//  InteballOfCell.m
//  MY Project For Sport
//
//  Created by dllo on 16/1/27.
//  Copyright © 2016年 dilo. All rights reserved.
//

#import "InteballOfCell.h"
#import "NetworkHandle.h"
#import "ModelForNBA.h"
#import "CellForRecommedImage.h"
#import "ModelForRecommend.h"
#import "CellForRecommendNews.h"
#import "CellForNBAAds.h"

#define WEIGHTT [UIScreen mainScreen].bounds.size.width / 375
#define HEIGHTT [UIScreen mainScreen].bounds.size.height / 667

@interface InteballOfCell ()<NetworkHandleDelegate, UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, retain) NSMutableArray *array;

@end


@implementation InteballOfCell

- (instancetype)initWithFrame:(CGRect)frame {
    
    self = [super initWithFrame:frame];
    if (self) {
        
       [self createTableView];
        [self handleData];
        
    }
    
    return self;
    
}

- (void)handleData {

NSString *str = @"http://platform.sina.com.cn/sports_client/feed?app_key=2586208540&_version__=3.2.1.01&__os__=android&feed_id=654&f=title%2Curl%2Ccategoryid%2Cimg%2Cimages%2Ccomment_total%2Ccomment_show%2Cctime%2Cvid%2Cvideo_info%2Cstitle%2Cwapsummary&num=20&partner=clear&pos=&sport_tour=&ad=1&pdps_params=%7B%22app%22%3A%7B%22timestamp%22%3A%221453811006599%22%2C%22os%22%3A%22Android%22%2C%22model%22%3A%22ZTE+G717C%22%2C%22device_type%22%3A%224%22%2C%22osv%22%3A%224.2.2%22%2C%22name%22%3A%22cn.com.sina.sports%22%2C%22carrier%22%3A%22%E4%B8%AD%E5%9B%BD%E7%94%B5%E4%BF%A1%22%2C%22device_id%22%3A%22A0000038A97F12%22%2C%22make%22%3A%22ZTE+G717C%22%2C%22channel%22%3A%22%22%2C%22connection_type%22%3A%222%22%2C%22version%22%3A30201001%2C%22ip%22%3A%22fe80%3A%3A4ecb%3Af5ff%3Afe3e%3Ae562%25wlan0%22%7D%7D&client_deviceid=7005145ee5bbe7f1f97c5e227f4ab771";

    [NetworkHandle handlerJSONWitURL:str delegate:self];

}

- (void)handleDidComplete:(id)result {

    self.array = [NSMutableArray array];
    
    NSDictionary *dic = [result objectForKey:@"result"];
    
    NSDictionary *dict = [dic objectForKey:@"data"];
    
    NSDictionary *dics = [dict objectForKey:@"feed"];
    
    NSArray *array = [dics objectForKey:@"data"];
    
    for (NSDictionary *dic in array) {
        
        ModelForNBA *model = [[ModelForNBA alloc] init];
        
        [model setValuesForKeysWithDictionary:dic];
        
        [self.array addObject:model];
        [model release];
        
    }
    
    [self.tableView reloadData];
    

}


- (void)createTableView {

    
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
     self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    [self.tableView registerClass:[CellForRecommendNews class] forCellReuseIdentifier:@"poolForInteBallNews"];
    [self.tableView registerClass:[CellForRecommedImage class] forCellReuseIdentifier:@"poolForInteBallImage"];
    
    [self.tableView registerClass:[CellForNBAAds class] forCellReuseIdentifier:@"poolForInteAds"];

    

}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    return self.array.count;

}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    ModelForNBA *model = [self.array objectAtIndex:indexPath.item];
    
    
    if ([model.categoryid isEqualToString:@"2"]) {
        
        ModelForRecommend *model2 = [self.array objectAtIndex:indexPath.item];
        CellForRecommedImage *cell = [tableView dequeueReusableCellWithIdentifier:@"poolForInteBallImage"];
        
        [cell passModelForRecommedImage:model2];
        
        return cell;
 
        
    } else if (([model.categoryid isEqualToString:@"1"] || [model.categoryid isEqualToString:@"3"])&& model.wapsummary != nil) {
        ModelForRecommend *model2 = [self.array objectAtIndex:indexPath.item];
        CellForRecommendNews *cell = [tableView dequeueReusableCellWithIdentifier:@"poolForInteBallNews"];
        
        [cell passModelForRecommend:model2];
        
        return cell;
    
    } else {

        CellForNBAAds *cell = [tableView dequeueReusableCellWithIdentifier:@"poolForInteAds"];
        
        [cell passModelForNBAAds:model];
        
        return cell;
       
    }


}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    ModelForRecommend *model = [self.array objectAtIndex:indexPath.item];
    
    if (([model.categoryid isEqualToString:@"1"] || [model.categoryid isEqualToString:@"3"])&& model.wapsummary != nil) {
        
        return self.contentView.frame.size.height / 5.5;
        
    } else if ([model.categoryid isEqualToString:@"2"]) {
        
        return self.contentView.frame.size.height / 4.5;
        
    } else {
        
        return 80 * HEIGHTT;
        
    }
    
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    [[NSNotificationCenter defaultCenter] postNotificationName:@"disappear" object:nil];


    if ([self.delegate respondsToSelector:@selector(didClickInteballRow:)]) {
        
        [self.delegate  didClickInteballRow:[self.array objectAtIndex:indexPath.row]];
        
    }


}





/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
